package com.crl.module.connecter.core
{
	import com.crl.module.connecter.handler.AbstractNetHandler;
	import com.crl.module.connecter.handler.INetHandler;
	import com.crl.module.connecter.model.CommandInfo;
	import com.crl.module.connecter.model.ConnectionInfo;
	
	import flash.events.EventDispatcher;
	import flash.net.Responder;

	public class Connecter extends EventDispatcher implements IConnecter,INetClient,INetSender
	{
		protected var handlers:Vector.<INetHandler>;
		protected var connInfo:ConnectionInfo;
		public function Connecter()
		{
			init();
		}
		
		protected function init():void{
			this.addHandler(new AbstractNetHandler());
		}
		
		
		
		public function connect(connInfo:ConnectionInfo):void{
			this.connInfo=connInfo	
		}
		public function close():void{
			throw new Error("子类未实现")
		}
		
		public function get connected():Boolean{
			return false;
		}
		
		public function receipt(commandInfo:CommandInfo):void{
			for each (var handler:INetHandler in this.handlers){
				handler.onData(this,commandInfo);
			}
		}
		
		public function send(commandInfo:CommandInfo,responder:Responder=null):void{
			
		}
		
		public function sends(comandInfos:Vector.<CommandInfo>):void{
			
		}
		
		
		public function addHandler(handler:INetHandler):int{
			if(handlers.indexOf(handler) !=-1)return -1;
			return handlers.push(handler);
		}
        public function removeHandler(handler:INetHandler):INetHandler{
        	return handlers.splice(handlers.indexOf(handler), 1) as INetHandler;
        }
		
	}
}